import datetime

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from common.utils import Driver
from common.utils import ScreenShot


class BlogEdit:
    def __init__(self):
        Driver.get("http://8.137.19.140:9090/blog_edit.html")
        Driver.implicitly_wait(10)

    # 检查页面元素
    def check_ele(self):
        Driver.find_element(By.CSS_SELECTOR, "#title")
        Driver.find_element(By.CSS_SELECTOR, "#submit")
        Driver.find_element(By.CSS_SELECTOR, "#editor")
        ScreenShot()

    # 成功发布文章
    def post_succeed(self):
        expected_title = "自动化测试-发布文章-" + datetime.datetime.now().strftime("%Y-%m-%d_%H_%M_%S")
        Driver.find_element(By.CSS_SELECTOR, "#title").send_keys(expected_title)
        # java可以通过Actions perform()键鼠操作删除或输入内容，python后续研究下，先不管内容直接发布（有默认内容）
        Driver.find_element(By.CSS_SELECTOR, "#submit").click()
        ScreenShot()
        # 成功后会跳转到主页
        assert Driver.current_url == "http://8.137.19.140:9090/blog_list.html"

    # 不输入标题，直接点击发布文章
    def post_no_title(self):
        Driver.find_element(By.CSS_SELECTOR, "#title").clear()
        Driver.find_element(By.CSS_SELECTOR, "#title").clear()
        Driver.find_element(By.CSS_SELECTOR, "#title").clear()
        Driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 警告窗口
        WebDriverWait(Driver, 1).until(expected_conditions.alert_is_present())
        Driver.switch_to.alert.accept()
        ScreenShot()
        assert Driver.current_url != "http://8.137.19.140:9090/blog_list.html"